package common.core.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 数据传递帮助类
 * @author 刘波
 *
 */
@ApiModel
public class Result<T> implements Serializable{

	private static final long serialVersionUID = 1L;
	@ApiModelProperty(value = "状态码,0为正常" , required = false , example = "0")
	public int code;
	/**系统操作完成返回参数-->成功:20000(操作)*/
	public static final int code_success=0;
	public static Result success(Object data,Class clazz){
		Object result = data;
		if(data instanceof IPage){
			IPage page = (IPage) data;
			List target =  CommonUtil.copyListBean(page.getRecords(), clazz);
			page.setRecords(target);
			result = page;
		}else if(data instanceof List) {
			List target = CommonUtil.copyListBean((List)data, clazz);
			result = target;
		}else if(data instanceof Object){
			result = CommonUtil.copyBean(data, clazz);
		}
		return  Result.success(result);
	}
	public static Result success(Object data){

		return new Result(0, "", data);
	}

	public static Result success(){
		return new Result(0, "", "");
	}

	/**系统操作完成返回参数-->失败:-1(自己抛错或者自己认为失败)*/
	public static final int CODE_FAIL =-1;
	public static Result fail(String msg){
		return new Result(CODE_FAIL, msg);
	}
	/**登录失败*/
	public static final int CODE_NOT_SIGN_ON=-2;
	public static Result failSignIn(String msg){
		return new Result(CODE_NOT_SIGN_ON, msg);
	}

	@ApiModelProperty(value = "提示消息" , required = false , example = "''")
	private String msg;
	@ApiModelProperty(value = "返回数据" , required = false , example = "{}")
	private T data;

	public Result(){
		super();
		this.code=0;
		this.msg="";
		this.data= null;
	}
	public Result(int code){
		super();
		this.code=code;
		this.msg="";
		this.data= null;
	}
	public Result(int code,String msg){
		super();
		this.code=code;
		this.msg=msg;
		this.data= null;
	}
	public Result(int code,String msg, T data){
		super();
		this.code=code;
		this.msg=msg;
		this.data=data;
	}

	public static Result result(int code){
		return new Result(code);
	}
	public static Result result(int code,String msg){
		return new Result(code, msg);
	}
	public static Result result(int code,String msg,Object data){
		return new Result(code, msg, data);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@SuppressWarnings("unchecked")
	public <T> T getData() {
		return (T)data;
	}
	@SuppressWarnings("all")
	public <T> T getData(Class<T> clazz) {
		if(data instanceof Map){
			return CommonUtil.convertMapToBean((Map)data, clazz);
		}
		return (T)data;
	}
	public void setData(T data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return CommonUtil.convertObjectToJson(this);
	}

	/**
	 * 是否为成功的
	 * @return
	 */
	public Boolean successful(){
		return 0 == this.code;
	}

	public Boolean notSuccessful(){
		return 0 != this.code;
	}
}


